package gold.digger;

import gold.utils.InputUtil;

import java.util.*;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC554 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int leastBricks(List<List<Integer>> wall) {
            List<Set<Integer>> crevices = new ArrayList<>();
            TreeSet<Integer> globalCreviceSet = new TreeSet<>();
            for (List<Integer> layer : wall) {
                TreeSet<Integer> curLayerCreviceSet = new TreeSet<>();
                int gapSum = 0;
                for (int i = 0; i < layer.size() - 1; i++) {
                    gapSum += layer.get(i);
                    curLayerCreviceSet.add(gapSum);
                    globalCreviceSet.add(gapSum);
                }
                crevices.add(curLayerCreviceSet);
            }

            int minBrick = Integer.MAX_VALUE;
            for (Integer point : globalCreviceSet) {
                int curPointBrick = 0;
                for (Set<Integer> crevice : crevices) {
                    if (!crevice.contains(point)) ++curPointBrick;
                }
                minBrick = Math.min(minBrick, curPointBrick);
            }

            return minBrick == Integer.MAX_VALUE ? wall.size() : minBrick;
        }
    }

    public void run() {
        Solution solution = new Solution();
        List<List<Integer>> arr = InputUtil.toDoubleIntegerList("[[1],[1],[1]]");

        System.out.println(solution.leastBricks(arr));
    }

    public static void main(String[] args) throws Exception {
        LC554 an = new LC554();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
